<?php
/**
 * --------------------------------------------------------------------
 * MCore框架
 * 作者：阿枫，QQ：52037872，Email：nczy520@gmail.com
 * --------------------------------------------------------------------
 *
 * SVN revision information:
 * @version $Revision: 50 $:
 * @author  $Author: zy $:
 * @date      $Date: 2013-11-09 19:34:15 +0800 (星期六, 09 十一月 2013) $:
 * @url         $HeadURL: svn://vps.mdeve.com/web_mcore/manage/function/global.func.php $:
 */
class _ConfigureClass extends __basic_def {

    private $template_path = "";

    function __construct(){
        $this->template_path = MANAGE_PATH. DIRECTORY_SEPARATOR. "templates";
    }

    /**
     * 编辑配置
     */
    function _edit(){
        global $_M;

        $args = $_GET["args"] ?? "";
        $params = $this->_decomposition_parameter($args);
        if(!array_key_exists("category", $params)) halt("设置类别错误！");
        $params["category"] = clear_string($params["category"]);

        $_M->db();
        $row = $_M->db->get_rows(array("table"=>"options", "where"=>"category='{$params["category"]}'", "order"=>"sort"));

        $_M->template();
        $_M->template->load("{$this->template_path}/options/configure_edit");
        //////////////////////////////////////////////////////////////////////////////////////////////////
        $options_text_bit = $_M->template->get_block("options_text_bit", true);
        $options_number_bit = $_M->template->get_block("options_number_bit", true);
        $options_textarea_bit = $_M->template->get_block("options_textarea_bit", true);
        $options_boolean_bit = $_M->template->get_block("options_boolean_bit", true);
        $options_select_bit = $_M->template->get_block("options_select_bit", true);
        $options_list = "";
        foreach($row as $r){
            $options_list_bit = "";
            ////////////////////////////////////////////////////////////////////////////////////////
            // 文本框
            if($r["field"] == "text"){
                $options_list_bit = $options_text_bit;
                foreach($r as $key=>$val){
                    $options_list_bit = str_replace("{". $key. "}", $val, $options_list_bit);
                }
            }
            ////////////////////////////////////////////////////////////////////////////////////////
            // 数字框
            if($r["field"] == "number"){
                $options_list_bit = $options_number_bit;
                foreach($r as $key=>$val){
                    $options_list_bit = str_replace("{". $key. "}", $val, $options_list_bit);
                }
            }
            ////////////////////////////////////////////////////////////////////////////////////////
            // 文本区域
            if($r["field"] == "textarea"){
                $options_list_bit = $options_textarea_bit;
                foreach($r as $key=>$val){
                    $options_list_bit = str_replace("{". $key. "}", $val, $options_list_bit);
                }
            }
            ////////////////////////////////////////////////////////////////////////////////////////
            // 单选按钮
            if($r["field"] == "boolean"){
                $options_list_bit = $options_boolean_bit;
                $r["checked1"] = $r["options_value"] ? " checked" : "";
                $r["checked0"] = $r["options_value"] ? "" :  " checked";
                foreach($r as $key=>$val){
                    $options_list_bit = str_replace("{". $key. "}", $val, $options_list_bit);
                }
            }
            ////////////////////////////////////////////////////////////////////////////////////////
            // 下拉列表
            if($r["field"] == "select"){
                $options_list_bit = $options_select_bit;
                $arr = explode(",", $r["script"]);
                $r["select_list"] = "";
                for($i=0; $i<sizeof($arr); $i++){
                    $arr1 = explode(":", $arr[$i]);
                    $r["select_list"] .= "<option value=\"$arr1[0]\"".($r["options_value"]==$arr1[0] ? " selected" : "").">".$arr1[1]."</option>";
                }
                foreach($r as $key=>$val){
                    $options_list_bit = str_replace("{{$key}}", $val, $options_list_bit);
                }
            }
            ////////////////////////////////////////////////////////////////////////////////////////
            $options_list .= $options_list_bit;
        }
        $_M->template->set_var("{options_list}", $options_list);
        //////////////////////////////////////////////////////////////////////////////////////////////////
        $breadcrumb = array(
            "系统设置" => "#",
            "编辑设置" => "",
        );
        $_M->template->set_var("{category}", $params["category"]);
        $_M->template->insert_html(_generate_breadcrumb($breadcrumb));
        $_M->template->display();
    }

    function _edit_save() {
        global $_M;
        $data = $_POST;

        $category = $data["category"] ?? "";
        if(!strlen($category)) ajax_result("error", "设置类别错误！");
        unset($data["category"]);

        $_M->db();
        $_M->db->startTrans();
        foreach($data as $key=>$val){
            if(!strlen($key)) continue;
            $key = clear_string($key);
            $val = clear_string($val);
            $row = $_M->db->get_row(array("table"=>"options", "field"=>"id", "where"=>"options_name='$key' and category='{$category}'"));
            if(false === $row) continue;
            $_M->db->update(array("options_value"=>$val), array("table"=>"options", "where"=>"id=$row[id]"));
        }
        $_M->db->commit();

        ajax_result("successful");
    }

}